import {formatDate} from "src/hrpub/common/utils/utils";
import {getSignStatus} from "./commonFun";

export default class FormAction {
    constructor(comp) {
        this.comp = comp;
    }

    formConfig = {
        area: 'attendance',
        formCode: 'attendance_approve_card'
    }
    // 表单进入浏览态
    toBrowseStatus = (formValue) => {
        const {props} = this.comp;
        const {
            attendance: {
                languagePage = {}
            },
            form, editTable, dispatch
        } = props;

        // 单据id
        let billId = '';

        // 表单进入编辑
        form.setFormStatus(this.formConfig.formCode, 'browse');
        // 填充表单的值
        if (formValue) {
            form.setAllFormValue({
                [this.formConfig.formCode]: formValue
            });
            billId = formValue.rows[0].values['pk_attendance'].value;
        }
        let batch_attend_list = {
            areacode: 'batch_attend_list',
            rows: []
        }
        if (formValue && formValue.rows && formValue.rows[0].values.fill_details
            && formValue.rows[0].values.fill_details.value) {
            let tempData = JSON.parse(formValue.rows[0].values.fill_details.value)
            tempData.forEach((item, index) => {
                let values = {
                    fillDate: {
                        value: typeof item['fillDate'] === 'number' ? formatDate(item['fillDate']): item['fillDate']
                    },
                    fillTime: {
                        value: formatDate(item['fillTime'], ':', 'hh-mm')
                    },
                    originalSignTime: {
                        value: item['originalSignTime']
                    },
                    listOrder: {
                        value: item['listOrder']
                    },
                    isNeedFill: {
                        value: ''
                    },
                    signState: {
                        value: item['originalSignStatus'],
                        display: getSignStatus(item['originalSignStatus'], languagePage)
                    },
                    originalSignStatus: {
                        value: item['originalSignState']
                    },
                    fillStatus: {
                        value: ''
                    },
                    fill_reason: {
                        value: item['fill_reason']
                    }
                };
                batch_attend_list.rows.push({
                    status: '0',
                    values
                })
            })

        }
        console.log(batch_attend_list)
        editTable.setTableData('batch_attend_list', batch_attend_list);
        dispatch({
            type: 'attendance/update',
            payload: {
                fileManagerBillId: billId
            }
        });
        // 按钮进入编辑的按钮
        /*this.update({
            addPageStatus: 'browse'
        })*/
    }

    // 直接进入预览页面
    toBrowsePage = async (id, formValue) => {
        let oneBillData = await this.getOneBillData(id);
        if (oneBillData) {
            this.toBrowseStatus(oneBillData.data[this.formConfig.area][this.formConfig.formCode]);
        }
    }

    // 获取一条数据
    getOneBillData = async (pk_attendance) => {
        const {props} = this.comp;
        const {dispatch, attendance} = props;
        try {
            let res = await dispatch({
                type: 'attendance/getBillDetail',
                payload: {
                    pk_attendance
                }
            });
            if (res.success) {
                return res;
            }
        } catch (e) {
        }
        return false;
    }
}